<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
    <title>归还管理</title>
    <meta name="decorator" content="default"/>
    <script type="text/javascript">
        $(function () {
            $('#banid').focus();

        });



        function selectAll() {
            $("input[name='check_item']").each(function () {
                if ($(this).attr('checked')) {
                    $(this).attr('checked', false);
                } else {
                    $(this).attr('checked', true);
                }
            });
        }

        function rback() {
            var o = $("input[name='check_item']:checked");
            if (o.length <= 0) {
                showTip("请选择书籍~");
            } else {
                if (confirm('是否归还？')) {
                    var ids = '';
                    o.each(function () {
                        ids += $(this).val() + ",";
                    });
                    if (ids != '') {
                        ids = ids.substring(0, ids.length - 1);
                    }
                    $('#bookIds').val(ids);
                    $('#queryform').attr('action', '${ctx}/book/borrowed/rtnbook').submit();
                }
            }
        }


        function f_delete2(id) {
            if (confirm('是否归还？')) {
                $('#bookIds').val(id);
                $('#queryform').attr('action', '${ctx}/book/borrowed/rtnbook').submit();
            }
        }


        //读者证书号查询
        function getbyreId() {
            getjson("#rdCardno", "请扫描或输入读者的编号~", "${ctx}/book/borrowed/rtnbookbyrdid", "暂无借阅记录~");
        }

        //书籍编号查询
        function getbybookId() {
            getjson("#banid", "请扫描或输入书籍的编号~", "${ctx}/book/borrowed/rtnbookbybookid", "暂无借阅记录~");
        }

        var subbmitFlag = true;

        //还书操作
        function returnBook() {
            //清空已经查询的结果集~
            $("div#rederinfo").children().remove();
            $("div#bookinfo").children().remove();
            var banid = $("#banid").val();
            if ("" == banid) {
                showTip("请扫描或输入书籍的编号!");
                return;
            }
                $.ajax({
                    type: "POST",
                    cache: false,
                    dataType: "json",
                    async: true,
                    url: "${ctx}/book/borrowed/returnBookByBanid",
                    data: $('#searchForm').serialize(),// 你的formid
                    success: function (json) {
                        if (null != json) {
                            var retFlag = json['retFlag'];//返回flag
                            if (retFlag != "success") {
                                showTip(retFlag);
                            } else {
                                showTip("归还成功！");
                            }
                        } else {
                            showTip("没有查询到借阅记录,请联系管理员!");
                        }
                        clearValue();
                    },

                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        showTip("系统异常，请稍后再查!");
                        return;
                    }
                });
        }


        function getjson(btnid, message1, url, message2) {
            //清空已经查询的结果集~
            $("div#rederinfo").children().remove();
            $("div#bookinfo").children().remove();
            var rdCardno = $(btnid).val();
            if ("" == rdCardno) {
                showTip(message1);
                return;
            }
            $.ajax({
                type: "POST",
                cache: false,
                dataType: "json",
                async: true,
                url: url,
                data: $('#searchForm').serialize(),// 你的formid
                success: function (json) {
                    if (null != json) {
                        json = eval(json);//将JSON的字符串解析成JSON数据格式
                        var rdId = "";//读者编号
                        var rdCardno = "";//借书证号
                        var rdName = "";//读者姓名
                        var rdTele = "";//读者电话
                        var readstatus = "";//读者状态		0:正常1：停用2：挂失
                        var rdCanBorrowDays = "";//借阅天数
                        var rdCanBorrowdBooks = "";//借阅本书
                        var rdContinueBorrowCount = "";//续借次数
                        var rdContinueBorrowDays = "";//续借天数
                        var borrowStatus = "";//图书借阅状态

                        var table_rederinfo = $("<table id=\"contentTable\" class=\"table table-striped table-bordered table-condensed\">");
                        table_rederinfo.appendTo($("#rederinfo"));

                        var table_bookinfo = $("<table id=\"contentTable\" class=\"table table-striped table-bordered table-condensed\">" +
                                "<tr><th><input type=\"checkbox\" name=\"selectalls\" onclick=\"selectAll()\"/>图书编号</th><th>书籍名称</th><th>续借次数</th><th>借阅记录</th><th>操作</th><tr>"
                        );
                        table_bookinfo.appendTo($("#bookinfo"));
                        $.each(json, function (index, item) {
                            //循环获取数据

                            //1.读者信息
                            rdId = json[index].rdId;//读者编号
                            rdCardno = json[index].rdCardno;
                            rdName = json[index].rdName;//读者姓名
                            rdTele = json[index].rdTele;//读者电话

                            rdstatus = "";//读者状态		0:正常1：停用2：挂失

                            var temp = json[index].status;

                            if (temp == "0") {
                                rdstatus = "正常";
                            } else if (temp == "1") {
                                rdstatus = "停用";
                            } else if (temp == "2") {
                                rdstatus = "挂失";
                            } else {
                                rdstatus = "未知";
                            }

                            rdCanBorrowDays = json[index].rdCanBorrowDays;//借阅天数
                            rdCanBorrowdBooks = json[index].rdCanBorrowdBooks;//借阅本书
                            rdContinueBorrowCount = json[index].rdContinueBorrowCount;//续借次数
                            rdContinueBorrowDays = json[index].rdContinueBorrowDays;//续借天数
                            var retFlag = json[index].retFlag;//返回flag
                            if (retFlag != "success") {
                                showTip(retFlag);
                                return;
                            }
                            //2.书籍信息
                            var bkId = json[index].bkId;//书籍编号
                            var banid = json[index].banid;//书籍编号
                            var bookname = json[index].bookname;//书籍名称
                            var contCnt = json[index].contCnt;//续借次数
                            var zlId = json[index].zlId;//借阅记录编号
                            borrowStatus = json[index].borrowStatus;//图书借阅标志 0：已归还 1：借出中
                            var tempStr = "<td><a href=\"#\" onclick=\"f_delete2(" + zlId + ")\"    title=\"归还\">归还</a></td><tr>";
                            if (borrowStatus == "0") {
                                tempStr = "<td>已归还</td><tr>";
                            }

                            var tablecont_bookinfo = $(
                                    "<tr><td><input type='checkbox' name='check_item' value=" + "'" + zlId + "'" + "/>" + banid + "</td><td>" + bookname + "</td><td>" + contCnt + "</td><td>" + zlId + "</td>" +
                                    tempStr
                            );
                            tablecont_bookinfo.appendTo(table_bookinfo);
                        });
                        var tablecont_readerinfo = $(
                                "<tr><td>借书证号</td><td>" + rdCardno + "</td><tr>" +
                                "<tr><td>读者姓名</td><td>" + rdName + "</td><tr>" +
                                "<tr><td>读者电话</td><td>" + rdTele + "</td><tr>" +
                                "<tr><td>读者状态</td><td>" + rdstatus + "</td><tr>" +
                                "<tr><td colspan=\"2\" >该读者可以借阅" + rdCanBorrowdBooks + "本，每本可以借阅" + rdCanBorrowDays + "天</td><tr>"
                        );

                        tablecont_readerinfo.appendTo(table_rederinfo);
                        var tempReturn = "<input id=\"delete_btn\" class=\"btn btn-primary\" type=\"button\" value=\"归还\" onclick=\"rback();\"/>";
                        if (borrowStatus == "0") {
                            tempReturn = "";
                        }

                        $("#bookinfo").append("</table>").append(tempReturn);
                        $("#rederinfo").append("</table>");
                    } else {
                        showTip(message2);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    showTip("系统异常，请稍后再查~");
                    return;
                }
            });

        }

        function clearValue() {
            $("#banid").val("");
            $("#banid").focus();
        }

        function getinfo(rdid) {
            window.open('${ctx}/book/borrowed/readerborrowlist?rdId=' + rdid, 'selected', 'directorys=no,toolbar=no,status=no,menubar=no,resizable=no,width=750,height=500,top=150,left=200,scrollbars=yes');
        }

        function page(n, s) {
            $("#pageNo").val(n);
            $("#pageSize").val(s);
            $("#searchForm").submit();
            return false;
        }
    </script>
</head>
<body>
<form:form id="searchForm" modelAttribute="borrow" action="#" method="post" class="breadcrumb form-search ">
    <input id="pageNo" name="pageNo" type="hidden" value="${page.pageNo}"/>
    <input id="pageSize" name="pageSize" type="hidden" value="${page.pageSize}"/>
    <sys:tableSort id="orderBy" name="orderBy" value="${page.orderBy}" callback="page();"/>
    <ul class="ul-form">
        <li>
            <label>借书证号：</label>
            <input name="rdCardno" id="rdCardno" value="" class="input-medium" onkeydown='if(event.keyCode==13){getbyreId()}'/>
        </li>

        <li class="btns"><input id="btnSubmit1" class="btn btn-primary" type="button" onclick="getbyreId()" value="扫描条码或者手动添加"/>
        <li class="clearfix"></li>
        <li>
            <label>图书条码：</label>
            <input name="banid" id="banid" value="" class="input-medium" onkeydown='if(event.keyCode==13){returnBook()}'/>
        </li>
        <li class="btns">
            <input id="btnSubmit2" class="btn btn-primary" type="button" onclick="returnBook()" value="扫描条码或者手动添加"/>
        <li class="clearfix"></li>
    </ul>
</form:form>


<sys:message content="${message}"/>
<form name="queryform" id="queryform" method="post" action="#" style="padding-bottom: 10px;padding-top: 10px;">

    <div id="rederinfo"></div>
    <div id="bookinfo"></div>
    <input type="hidden" name="bookIds" id="bookIds" value=""/>
</form>


</body>
</html>